﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.BLL.Accounts;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Common;
using Jzy.Core.Common.Basic;
using FortuneHeaven.Entity.Accounts;
using Jzy.Core.Utility;
using FortuneHeaven.BLL.NetBar;
using FortuneHeaven.Entity.NetBar;

namespace FortuneHeaven.Admin.Pages.Accounts
{
    public partial class List : PageList
    {
        private AccountsBO Accounts
        {
            get
            {
                return AccountsBO.Instance<AccountsBO>();
            }
        }

        private int? _netBarID;
        private int NetBarID
        {
            get
            {
                if (_netBarID == null)
                {
                    UserExBO user = new UserExBO();
                    UserExInfo info = user.GetInfo(AppSession.UserID);
                    if (info != null)
                    {
                        _netBarID = info.NetBarID;
                    }
                    else
                    {
                        _netBarID = 0;
                    }
                }
                return _netBarID.Value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["Name"]))
                {
                    txtName.Text = Request.QueryString["Name"];
                }

                

                BindView();
            }
            base.InitSelectID(hidSelectIDs);
            AddAttr();
        }

        private void BindView()
        {

            if (NetBarID != 0)
            {
                NetBarBO netbar = new NetBarBO();
                NetBarInfo info = netbar.GetInfo(NetBarID);
                litNetBar.Text = "&nbsp;-&nbsp;&nbsp;本网吧当前积分：[&nbsp;<span style='color:red'>" + info.Score + "</span>&nbsp;]";
            }

            LVUtil.Init(lv, lvHeader);
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = AppConfig.PageSize;
            setting.OrderBy = "RegisterDate desc";
            setting.Where = "NetBarID=@p0";
            if (txtName.Text.Trim() != "")
            {
                setting.Where += " and Accounts like @p1 ";
            }

            string prefix = GlobalVar.WebCurrPath + "List.aspx?Name=" + Server.UrlEncode(txtName.Text.Trim());
            List<AccountsInfo> list = this.Accounts.GetList(setting, NetBarID, "%" + txtName.Text.Trim() + "%");
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();

            litPager.Text = HtmlPager.Render(setting, prefix, true);

        }

        private void AddAttr()
        {
            //JSUtil.ShowForm(lnkAdd, "增加网吧", null, GlobalVar.WebCurrPath + "Edit.aspx", 400, 262);
            //JSUtil.ClickBtn(lnkDelete, btnDelete);
            //JSUtil.ClickBtn(lnkStartup, btnStartup);
            //JSUtil.ClickBtn(lnkStop, btnStop);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                this.Accounts.Delete(StrUtil.SplitToStr(strSelectIDs));
            }
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                //LVUtil.SetEdit(sender, e, 400, 262);
                int userID = ((e.Item as ListViewDataItem).DataItem as AccountsInfo).UserID;

                HyperLink lnkAddScore = e.Item.FindControl("lnkAddScore") as HyperLink;
                string url = GlobalVar.WebCurrPath + "AddScore.aspx?NetBarID=" + NetBarID + "&AccountID=" + userID;
                JSUtil.ShowForm(lnkAddScore, "充积分", null, url, 350, 231);


                HyperLink lnkSubScore = e.Item.FindControl("lnkSubScore") as HyperLink;
                url = GlobalVar.WebCurrPath + "SubScore.aspx?NetBarID=" + NetBarID + "&AccountID=" + userID;
                JSUtil.ShowForm(lnkSubScore, "充积分", null, url, 350, 231);
            }
        }

        protected void btnStartup_Click(object sender, EventArgs e)
        {
            List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
            //this.NetBar.Startup(lstMemberIDs);
            BindView();
        }
        protected void btnStop_Click(object sender, EventArgs e)
        {
            List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
            // NetBar.Stop(lstMemberIDs);
            BindView();
        }
    }
}